package com.cn.pushpall;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by chenyuncong on 2019/8/16.
 */
public class SimpleLock {
    private static final ConcurrentHashMap<Integer,Integer> map2 =new ConcurrentHashMap<>();
    private static final Map<Integer,Integer> map =new HashMap<>();

    public static Integer lock(Integer id){
        int val=0;
        synchronized (id){
            if(!map.containsKey(id)){
                map.put(id,0);
            }
            val=map.get(id)+1;
            map.put(id,val);
        }
        return val;
    }

    public static void unlock(Integer id){
        synchronized (id){
            map.remove(id);
        }
    }

    public static void main(String[] args) {
        for(int i=0;i<10;i++){
            new Thread(new TaskRun(i)).start();
        }
        for(int i=0;i<10;i++){
            new Thread(new TaskRun(i)).start();
        }
        for(int i=0;i<10;i++){
            new Thread(new TaskRun(i)).start();
        }

    }
    static class TaskRun implements Runnable{
        private Integer id;
        TaskRun(Integer id){
            this.id=id;
        }
        @Override
        public void run() {
            System.out.println(id+"--"+lock(id));
        }
    }
}
